<?php
require_once 'pants/common.php';

$location = explode('/', Pants::getResponse('location', '/index'), 16);

// The first element is always empty due to the leading slash.
array_shift($location); 

switch($page = array_shift($location)) {
  // -- Index ------------------------------------------------------------------
  case 'index':
    $pager = new PantsPager(PantsArticle::getCount());
    
    Pants::render('index', array(
      'articles'       => PantsArticle::getRecent(),
      'mode'           => PANTS_COMPACT,
      'pageCurrent'    => $pager->page,
      'pageTotal'      => $pager->total,
      'pageTotalPages' => $pager->totalPages,
      'pagerUrl'       => SITE_URL.'/archive',
    ));
    break;

  // -- Archive ----------------------------------------------------------------
  case 'archive':
    $pager = new PantsPager(PantsArticle::getCount(), array_shift($location),
        10);

    Pants::render('archive', array(
      'articles'       => PantsArticle::getRecent(10, $pager->offset),
      'mode'           => PANTS_COMPACT,
      'pageCurrent'    => $pager->page,
      'pageTitle'      => 'Archive',
      'pageTotal'      => $pager->total,
      'pageTotalPages' => $pager->totalPages,
      'pagerUrl'       => SITE_URL.'/archive',
    ));
    break;

  // -- Article ----------------------------------------------------------------
  case 'article':
    if (!$id = array_shift($location)) {
      Pants::error404();
    }

    $comment = new PantsComment(0, $id, 0, Pants::getCookie('author'),
      Pants::getCookie('email'), Pants::getCookie('url'));

    switch($action = Pants::getResponse('action')) {
      case 'Preview Comment':
        $comment = PantsComment::createFromResponse();
        Pants::$parameters['previewComment'] = 'yes';
        break;

      case 'Post Comment':
        $comment = PantsComment::createFromResponse();

        if ($comment && $comment->save()) {
          header("Location: ".SITE_URL."/article/$id#comment-$comment->id");
          exit;
        }
        else {
          Pants::render('error', array(
            'pageTitle'    => 'Error',
            'errorMessage' => 'There was an error posting your comment. '.
              'Please try again later.'
          ));
        }
        break;
    }

    // Display the article.
    try {
      $article = PantsArticle::getById($id);
    }
    catch (Exception $e) {
      Pants::error404();
    }

    Pants::render('article', array(
      'article'    => $article,
      'mode'       => PANTS_FULL,
      'newComment' => $comment,
      'pageTitle'  => $article->title,
      'feedAtom'   => PAGE_URL.'?type=atom',
      'feedRSS'    => PAGE_URL.'?type=rss'
    ));
    break;

  // -- Login ------------------------------------------------------------------
  case 'login':
    try {
      Pants::login();
    }
    catch (PantsLoginException $e) {
      Pants::error('Login Error', $e->getMessage());
    }
    
    header('Location: '.Pants::getResponse('originalUrl', SITE_URL));
    exit;
    break;
  
  // -- Logout -----------------------------------------------------------------
  case 'logout':
    Pants::logout();
    header('Location: '.Pants::getResponse('originalUrl', SITE_URL));
    exit;
    break;

  // -- Page -------------------------------------------------------------------
  case 'page':
    try {
      $page = PantsPage::getByName(array_shift($location));
    }
    catch (Exception $e) {
      Pants::error404();
    }

    Pants::render('page', array(
      'mode'      => PANTS_FULL,
      'page'      => $page,
      'pageTitle' => $page->title,
    ));
    break;


  // -- Recent Comments --------------------------------------------------------
  case 'recent-comments':
    Pants::render('recent-comments', array(
      'comments'  => PantsComment::getRecent(),
      'mode'      => PANTS_TINY,
      'pageTitle' => 'Recent Comments',
    ));
    break;

  // -- Search -----------------------------------------------------------------
  case 'search':
    $query = trim(Pants::getResponse('query'));
    
    Pants::render('search', array(
      'articles'    => PantsArticle::search($query),
      'comments'    => PantsComment::search($query),
      'feedAtom'    => PAGE_URL.'&type=atom',
      'feedRSS'     => PAGE_URL.'&type=rss',
      'mode'        => PANTS_TINY,
      'pages'       => PantsPage::search($query),
      'pageTitle'   => "Search: $query",
      'searchQuery' => $query,
      'tags'        => PantsTag::search($query),
    ));
    break;

  // -- Tag --------------------------------------------------------------------
  case 'tag':
    $tag_name = array_shift($location);
    $page     = array_shift($location);

    $tag      = PantsTag::getByName($tag_name);
    $pager    = new PantsPager($tag->getArticleCount(), $page, 10);

    Pants::render('tag', array(
      'articles'       => $tag->getArticles(10, $pager->offset),
      'mode'           => PANTS_COMPACT,
      'tagName'        => $tag->name,
      'feedAtom'       => PAGE_URL.'?type=atom',
      'feedRSS'        => PAGE_URL.'?type=rss',
      'pageCurrent'    => $pager->page,
      'pageTitle'      => $tag->name,
      'pageTotal'      => $pager->total,
      'pageTotalPages' => $pager->totalPages,
      'pagerUrl'       => SITE_URL.'/tag/'.urlencode($tag->name),
    ));
    break;

  default:
    Pants::error404();
}
?>